package com.losl.framework.web.exception;

import com.losl.common.core.domain.Result;
import com.losl.common.exception.BaseException;
import com.losl.common.exception.CustomException;
import com.losl.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理类
 * @author Maxwell
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(BaseException.class)
    public Result baseExceptionHandle(BaseException e){
        logger.error(e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(CustomException.class)
    public Result customExceptionHandle(CustomException e){
        if (StringUtils.isNull(e.getCode())){
            return Result.error(e.getMessage());
        }
        return Result.error(e.getCode(),e.getMessage());
    }

}
